<?php
error_reporting(0) ;
date_default_timezone_set('Asia/Shanghai');
header("Content-Type: text/html;charset=utf-8");
include '360_safe3.php';

define('THINK_PATH','/');
$db_config=require('../Conf/config.php');
		//MD5私钥 
//$MD5key = "IUNf";	//直接获取后台设置的
//$key="deyMOf";
		//配置MYSQL数据库连接信息
		$mysql_server_name	=	$db_config['DB_HOST'];//"111"; 	//数据库服务器名称
		$mysql_username		=	$db_config['DB_USER'];//"111"; 	// 连接数据库用户名
		$mysql_password		=	$db_config['DB_PWD'];//"123456";		// 连接数据库密码
		$mysql_database		=	$db_config['DB_NAME'];//"111"; 		// 数据库的名字

$mysql_charset='gbk';
	//支付宝订单号 
	$tno=$_GET["tno"];
	//编号备注 一般是用户名
	$payno = $_GET["payno"];
	//金额
	$money = $_GET["money"];
	//取得的MD5校验信息
	$SignMD5info = $_GET["sign"]; 
	//KEY
	$getkey = $_GET["key"];

if(!$tno){exit('没有交易单号');}
if(!$payno){exit('没有编号');}
if(!$getkey||!$SignMD5info){exit('参数错误');}

$mysql_conn = mysql_connect($mysql_server_name, $mysql_username, $mysql_password);
if (!$mysql_conn){exit('数据库错误');}
			mysql_select_db($mysql_database, $mysql_conn);
	mysql_set_charset($mysql_charset);
	
if(!$sys=mysql_fetch_array(mysql_query("select * from t_sys where id=1 limit 1"))){exit('获取系统配置错误');}
$key=$sys['ikey'];
$MD5key=$sys['skey'];

if($getkey<>$key){//KEY校验
	echo 'KEY错误';exit;
}
  $md5src = $tno.$payno.$money.$MD5key;
  //MD5签名检验结果
	$md5sign = strtoupper(md5($md5src));
//	echo $md5sign;
#	校验码正确.
if($md5sign!=$SignMD5info){exit('签名错误');}

 
 if(!$data=mysql_fetch_array(mysql_query("select * from t_cz_apply where typeid=8 and orderno='$payno' limit 1"))){//编号号是否存在
	exit('编号不存在');
	}
if($data['status']<>0){exit('已经支付');}
	$upsql="update t_cz_apply  set goldnum='$money',status=1 where orderno='$payno' and userid='$data[userid]' and typeid=8";// 
$up1=mysql_query($upsql);
if(!$up1){exit('更新错误');}

$PayTime=date("Y-m-d H:i:s",time());
$insql="INSERT into t_chong_zhi_log (userid,typeid,goldnum,addtime) values('$data[userid]',8,'$money','$PayTime')";
$in1=mysql_query($insql);
if(!$in1){exit('插入错误');}

$sql="update t_chong_zhi  set goldnum=goldnum+'$money' where userid='$data[userid]' and typeid=8";
$up2=mysql_query($sql);
if(!$up2){exit('更新金额错误');}
// 三级返利
$award1=$sys['award1'];
$award2=$sys['award2'];
$award3=$sys['award3'];
$Time=time();
/*$user=mysql_fetch_array(mysql_query("select * from t_user where  id='$data[userid]' limit 1"));
if($user['invit']){
$user1=mysql_fetch_array(mysql_query("select * from t_user where  inviturl='$user[invit]' limit 1"));
if($award1){//上级
	$fmoney=$money*$award1/100;
	//$insql="INSERT into t_chong_zhi_log (userid,typeid,goldnum,addtime) values('$user1[id]',8,'$fmoney','$PayTime')";
	$insql="INSERT into t_factory_jiangli (user,upuser,money,time,type) values('$user[id]','$user1[id]','$fmoney','$Time',8)";
$in1=mysql_query($insql);
if(!$in1){exit('插入返点1错误');}

$sql="update t_chong_zhi  set goldnum=goldnum+'$fmoney' where userid='$user1[id]' and typeid=8";
$up2=mysql_query($sql);
if(!$up2){exit('更新上级金额错误');}
}
}

if($user1['invit']){
$user2=mysql_fetch_array(mysql_query("select * from t_user where  inviturl='$user1[invit]' limit 1"));
if($award2){//上上级
		$fmoney=$money*$award2/100;
	//$insql="INSERT into t_chong_zhi_log (userid,typeid,goldnum,addtime) values('$user2[id]',8,'$fmoney','$PayTime')";
	$insql="INSERT into t_factory_jiangli (user,upuser,money,time,type) values('$user[id]','$user2[id]','$fmoney','$Time',8)";
$in1=mysql_query($insql);
if(!$in1){exit('插入返点2错误');}

$sql="update t_chong_zhi  set goldnum=goldnum+'$fmoney' where userid='$user2[id]' and typeid=8";
$up2=mysql_query($sql);
if(!$up2){exit('更新上上级金额错误');}
}
}

if($user2['invit']){
$user3=mysql_fetch_array(mysql_query("select * from t_user where  inviturl='$user2[invit]' limit 1"));
if($award3){//上上上级
		$fmoney=$money*$award3/100;
	//$insql="INSERT into t_chong_zhi_log (userid,typeid,goldnum,addtime) values('$user3[id]',8,'$fmoney','$PayTime')";
	$insql="INSERT into t_factory_jiangli (user,upuser,money,time,type) values('$user[id]','$user3[id]','$fmoney','$Time',8)";
$in1=mysql_query($insql);
if(!$in1){exit('插入返点2错误');}

$sql="update t_chong_zhi  set goldnum=goldnum+'$fmoney' where userid='$user3[id]' and typeid=8";
$up2=mysql_query($sql);
if(!$up2){exit('更新上上上级金额错误');}
}
}*/
//已返点
$insql="INSERT into t_orderno_flag (userid,orderno,addtime) values('$data[userid]','$payno','$Time')";
$in1=mysql_query($insql);
if(!$in1){exit('插入已返点错误');}

exit('1');
?>